using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;

using System.Web.Caching;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ACM.cmsarea08.Controls
{
    [DefaultProperty("Src")]
    [ToolboxData("<{0}:EmbedInput runat=server></{0}:EmbedInput>")]
    public class EmbedInput : Control
    {

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string InstanceID
        {
            get
            {
                object o = ViewState["instance_id"];
                if (o == null) return string.Empty;
                else return o.ToString();
            }
            set
            {
                ViewState["instance_id"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string SaveID
        {
            get
            {
                object o = ViewState["save_id"];
                if (o == null) return string.Empty;
                else return o.ToString();
            }
            set
            {
                ViewState["save_id"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string HandledPath
        {
            get
            {
                object o = ViewState["HandledPath"];
                if (o == null) return string.Empty;
                else return o.ToString();
            }
            set
            {
                ViewState["HandledPath"] = value;
            }
        }


		public EmbedInput()
		{
            

			
		}
		
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            string embedobjecturl = "/template/plugins/embedcontrol/EmbedObject.js";//Page.ClientScript.GetWebResourceUrl(typeof(EmbedInput), "ACM.cmsarea08.Controls.Resources.EmbedObject.js");
            string cssembedobjecturl = "/template/plugins/embedcontrol/EmbedObject.css";//Page.ClientScript.GetWebResourceUrl(typeof(EmbedInput), "ACM.cmsarea08.Controls.Resources.EmbedObject.css");
           // HttpContext.Current.Response.Write("--> " + embedobjecturl);

            if(this.Page is ACM.cmsarea08.Core.MainPage) {
//            	((ACM.cmsarea08.Core.MainPage)this.Page).RegisterMooTools();
                ((ACM.cmsarea08.Core.MainPage)this.Page).RegisterCustomJavascript("/template/plugins/mootools-1.2-core.js");
                ((ACM.cmsarea08.Core.MainPage)this.Page).RegisterCustomJavascript("/template/plugins/mootools-1.2-more.js");
                ((ACM.cmsarea08.Core.MainPage)this.Page).RegisterCustomJavascript(embedobjecturl);
                ((ACM.cmsarea08.Core.MainPage)this.Page).RegisterCustomCss(cssembedobjecturl);
            } else {
            	//includi lo script... mootools..
            	if(this.Page.Header!=null) {
            		System.Web.UI.HtmlControls.HtmlHead myhead = this.Page.Header;
					bool foundMo = false;
                    bool foundMoMore = false;
					bool foundEb = false;
                    bool foundEbc = false;
					for(int i = 0; i <myhead.Controls.Count;i++) {
						if(myhead.Controls[i] is LiteralControl) {
                            if (((LiteralControl)myhead.Controls[i]).Text.ToLower().IndexOf("mootools-1.2-core.js") != -1)
                            {
								foundMo=true;
							}
                            if (((LiteralControl)myhead.Controls[i]).Text.ToLower().IndexOf("mootools-1.2-more.js") != -1)
                            {
                                foundMoMore = true;
                            }

							if(((LiteralControl)myhead.Controls[i]).Text.ToLower().IndexOf("embedobject.js")!=-1) {
								foundEb=true;
							}
                            if (((LiteralControl)myhead.Controls[i]).Text.ToLower().IndexOf("embedobject.css") != -1)
                            {
                                foundEbc = true;
                            }

						}
					}
					if(!foundMo) {
                        LiteralControl ct = new LiteralControl("<script language=\"javascript\" type=\"text/javascript\" src=\"/template/plugins/mootools-1.2-core.js\"></script>");
						myhead.Controls.Add(ct);			
					}
                    if (!foundMoMore)
                    {
                        LiteralControl ct = new LiteralControl("<script language=\"javascript\" type=\"text/javascript\" src=\"/template/plugins/mootools-1.2-more.js\"></script>");
                        myhead.Controls.Add(ct);
                    }            		

					if(!foundEb) {
						LiteralControl ct = new LiteralControl("<script language=\"javascript\" type=\"text/javascript\" src=\""+embedobjecturl+"\"></script>");
						myhead.Controls.Add(ct);			
					}
                    if (!foundEbc)
                    {
                        LiteralControl ct = new LiteralControl("<style type=\"text/css\" media=\"screen\">\n"+
                            "<!--\n"+
                            "@import url(\""+cssembedobjecturl+"\");\n"+
                            "-->\n</style>");
                        myhead.Controls.Add(ct);
                    }             		
            		
            	}
            }
        }		
        protected override void Render(HtmlTextWriter writer)
        {
			//this.ID
			//this.ClientID
			//this.UniqueID
			writer.Write("<div id=\""+this.ClientID+"\" class=\"embedControl\" >");
            writer.Write("<div class=\"leftAre\"><h3>Media List <img id=\"helpList" + this.ClientID + "\" src=\"/administration/template/newimg/help.gif\" width=\"14\"/></h3>");
			writer.Write("<select id=\""+this.ClientID+"_lister\" size=\"10\">");
		    writer.Write("<option value=\"1\">one</option>");
			writer.Write("</select>");
            writer.Write("<input type=\"button\" id=\"" + this.ClientID + "_addbtm\" value=\"add\" class=\"embcmd\" />");
            writer.Write("<input type=\"button\" id=\"" + this.ClientID + "_prebtm\" value=\"pre\" class=\"embcmd\" />");
            writer.Write("<input type=\"button\" id=\"" + this.ClientID + "_delbtm\" value=\"del\" class=\"embcmd\" />");
		    writer.Write("</div>");
            writer.Write("<div class=\"rightAre\"><h3>Insert/Edit Media <img id=\"helpEdit" + this.ClientID + "\" src=\"/administration/template/newimg/help.gif\" width=\"14\"/></h3>");
			writer.Write("<label for=\""+this.ClientID+"_inputTitle\">Title</label>");
		    writer.Write("<input type=\"text\" id=\""+this.ClientID+"_inputTitle\" class=\"embcmd\" />");
		    writer.Write("<label for=\""+this.ClientID+"_inputEmb\">Embed HTML</label>");
		    writer.Write("<textarea id=\""+this.ClientID+"_inputEmb\"></textarea>");
            writer.Write("<input type=\"button\" id=\"" + this.ClientID + "_savebtm\" value=\"save current object\" class=\"embcmd\" />");
		    writer.Write("</div>");
		    writer.Write("</div>\n");

            string inst = this.InstanceID;
            if (inst == string.Empty) inst = "1";
            string hand = this.HandledPath;
            if (hand == string.Empty) hand = "/controls/medins/handler.aspx";
		    writer.Write("<script language=\"javascript\" type=\"text/javascript\" defer=\"defer\">\n");
		    writer.Write("window.addEvent('domready', function() {\n");
            writer.Write("var " + this.ID + "imoEl = new EmbedderObj( {panelID:'" + this.ClientID + "', instanceid:'" + inst + "', " +
		                 "saveid:'" + (this.SaveID!=string.Empty?this.SaveID:this.InstanceID) + "', " +
		                 "tempid:'90x', handlerPath:'" + hand + "'});\n");
		    writer.Write("});");
            writer.Write("</script>\n");
		    
		    
		    
		    
		    

        }
        
	}
}
